package com.cjl.dental_clinic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjl.dental_clinic.base.BaseController;
import com.cjl.dental_clinic.base.ResVo;
import com.cjl.dental_clinic.entity.Medicine;
import com.cjl.dental_clinic.service.MedicineService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: cjl
 * @time: 2024/04/04 15:43
 */
@RestController
@RequestMapping("cjl/medicine")
public class MedicineController extends BaseController<Medicine, MedicineService> {
    public MedicineController(MedicineService baseService) {
        super(baseService);
    }

    @PostMapping("list")
    public ResVo<List<Medicine>> list(String name) {
        if (!StringUtils.hasText(name)) {
            return ResVo.ok("查询成功",baseService.list());
        }
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        return ResVo.ok("查询成功",baseService.list(queryWrapper));
    }

    @PostMapping("del")
    public ResVo<Boolean> del(Integer id){
        return ResVo.ok(baseService.removeById(id));
    }
    @PostMapping("add")
    public ResVo<Boolean> add(Medicine medicine){
        medicine.initCreateUser();

        return ResVo.ok(baseService.save(medicine));
    }
    @PostMapping("upData")
    public ResVo<Boolean> updata(Medicine medicine){
        medicine.initUpdateUser();
        return ResVo.ok(baseService.updateById(medicine));
    }
}
